// Copyright (C) 1991 - 1999 Rational Software Corporation

#include "stdafx.h"
#include "CMD5.h"
#include "MD5.h"



//##ModelId=4430F22400EB
CMD5::CMD5()
{
	// TODO: Add your specialized code here.
}

//##ModelId=4430F22400EC
CMD5::~CMD5()
{
	// TODO: Add your specialized code here.
}


char *MDFile(const char *filename)
{
		OutputDebugString("start:\r\n");

		static char output[33]={""};
        FILE *file;
        MD5_CTX context;
        int i, len;
        unsigned char buffer[1024], digest[16];
        char output1[32];
        if ((file = fopen (filename, "rb")) == NULL)
        {
                printf ("%s can't be openedn", filename);
                return 0;
        }
        else
        {
                MD5Init (&context);
                while (len = fread (buffer, 1, 1024, file))
                {
                    MD5Update (&context, buffer, len);
                }
                MD5Final (digest, &context);
                fclose (file);

                for (i = 0; i < 16; i++)
                {
                      sprintf(&(output1[2*i]),"%02x",(unsigned char)digest[i]);
                      sprintf(&(output1[2*i+1]),"%02x",(unsigned char)(digest[i]<<4));
                }

				for(i=0;i<32;i++)
					output[i]=output1[i];

				return output;
		}
	return 0;
}

//##ModelId=4430F22400ED
VOID CMD5::MD5OfFile(const char *strFileName, BYTE pDigest[16])
{
	// TODO: Add your specialized code here.
	// NOTE: Requires a correct return value to compile.
	MD5_CTX ctxData;

	MD5Init(&ctxData);

	BYTE	pBuff[LENGTH_EACHREAD];
	DWORD	dwActualBuffLen = 0;
	FILE*	fStream;

	if( (fStream = fopen( strFileName, "rb" )) == NULL )
	{
		perror( "Can not find the file!" );
		return;
	}

	/* Cycle until end of file reached: */
	while( !feof( fStream ) )
	{
		dwActualBuffLen = (DWORD)fread( pBuff, sizeof( BYTE ), LENGTH_EACHREAD, fStream );
		if( ferror( fStream ) )     
		{
			AfxMessageBox("Read error",MB_OK);
			fclose(fStream);
			break;
		}

		MD5Update(&ctxData, pBuff, dwActualBuffLen);
	}
	
	fclose( fStream );


	MD5Final(pDigest, &ctxData);
}

//##ModelId=4430F22400F0
VOID CMD5::MD5OfBuff(BYTE* pBuff, DWORD dwBuffSize, BYTE pDigest[16])
{
	// TODO: Add your specialized code here.
	// NOTE: Requires a correct return value to compile.
	MD5_CTX ctxData;

	MD5Init(&ctxData);
	MD5Update(&ctxData, pBuff, dwBuffSize);
	MD5Final(pDigest, &ctxData);
}

